<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch">
      <el-form-item label="sim卡iccid号" prop="simIccid" width="30%">
        <el-input
          v-model.trim="queryParams.simIccid"
          placeholder="请输入sim卡iccid号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="sim卡运营商" prop="simTeleOpr" width="30%">
        <el-select v-model="queryParams.simTeleOpr" placeholder="请选择sim卡运营商" clearable size="small">
          <el-option
            v-for="dict in simTeleOprOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="cyan" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['terminal:sim:add']"
        >添加</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['terminal:sim:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['terminal:sim:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['terminal:sim:export']"
        >导出</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="info"
          icon="el-icon-refresh"
          size="mini"
          :disabled="multiple"
          @click="handleRefresh"
          v-hasPermi="['terminal:sim:refresh']"
        >更新</el-button>
      </el-col>
	  <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="simList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="45px" align="center" />
      <el-table-column label="iccid号" align="center" prop="simIccid" min-width="195%"/>
      <el-table-column label="msisdn号" align="center" prop="simMsisdn" min-width="155%"/>
      <el-table-column label="imsi号" align="center" prop="simImsi" min-width="155%"/>
      <el-table-column label="运营商" align="center" prop="simTeleOpr" :formatter="simTeleOprFormat" min-width="85%"/>
      <el-table-column label="类别" align="center" prop="simKind" :formatter="simKindFormat" min-width="85%"/>
      <el-table-column label="绑定终端的imei号" align="center" prop="termImei" min-width="160%"/>
      <el-table-column label="余额(元)" align="center" prop="simAcctBal" min-width="90%"/>
      <el-table-column label="已用流量(MB)" align="center" prop="simFlowUsedM" min-width="120%"/>
      <el-table-column label="开卡日期" align="center" prop="simOpenDate" min-width="105%">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.simOpenDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="激活日期" align="center" prop="simActiveDate" min-width="105%">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.simActiveDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="创建时间" align="center" prop="gmtCreate" min-width="140%">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.gmtCreate, '{y}-{m}-{d} {h}:{i}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="修改时间" align="center" prop="gmtModified" min-width="140%">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.gmtModified, '{y}-{m}-{d} {h}:{i}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" min-width="95%">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-refresh"
            @click="handleRefresh(scope.row)"
            v-hasPermi="['terminal:sim:refresh']"
          >更新</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改sim卡管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="106px" label-position="left">
        <el-form-item label="sim卡iccid号" prop="simIccid" v-if="showIccid">
          <el-input v-model.trim="form.simIccid" placeholder="请输入sim卡iccid号" />
        </el-form-item>
        <el-form-item label="sim卡运营商" prop="simTeleOpr">
          <el-select v-model="form.simTeleOpr" placeholder="请选择sim卡运营商">
            <el-option
              v-for="dict in simTeleOprOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="sim卡类别" prop="simKind">
          <el-select v-model="form.simKind" placeholder="请选择sim卡类别">
            <el-option
              v-for="dict in simKindOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="创建时间" prop="gmtCreate" v-if="showCreate">
          <el-input v-model="form.gmtCreate" placeholder="" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listSim, getSim, delSim, addSim, updateSim, refreshSim } from "@/api/terminal/sim";

export default {
  showCreate: false,
  name: "Sim",
  components: {
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 表单默认显示iccid
      showIccid: true,
      // 表单默认不显示创建时间
      showCreate: false,
      // 总条数
      total: 0,
      // sim卡管理表格数据
      simList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 运营商字典
      simTeleOprOptions: [],
      // sim卡类别字典
      simKindOptions: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        simIccid: null,
        simTeleOpr: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        simIccid: [
          { required: true, message: "sim卡的iccid号不能为空", trigger: "blur" },
          { max: 20, message: "sim卡的iccid号长度不能超过20位", trigger: "blur" }
        ]
      }
    };
  },
  created() {
    this.getList();
    this.getDicts("sim_tele_opr").then(response => {
      this.simTeleOprOptions = response.data;
    });
    this.getDicts("sim_kind").then(response => {
      this.simKindOptions = response.data;
    });
  },
  methods: {
    /** 查询sim卡管理列表 */
    getList() {
      this.loading = true;
      listSim(this.queryParams).then(response => {
        this.simList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 运营商字典翻译
    simTeleOprFormat(row, column) {
      return this.selectDictLabel(this.simTeleOprOptions, row.simTeleOpr);
    },
    // sim卡类别字典翻译
    simKindFormat(row, column) {
      return this.selectDictLabel(this.simKindOptions, row.simKind);
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        simIccid: null,
        simMsisdn: null,
        simImsi: null,
        simTeleOpr: null,
        simKind: null,
        termImei: null,
        simAcctBal: null,
        simFlowUsedM: null,
        simOpenedDate: null,
        simActivatedDate: null,
        gmtCreate: null,
        gmtModified: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.simIccid)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.showIccid = true;
      this.title = "添加sim卡";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const simIccid = row.simIccid || this.ids
      getSim(simIccid).then(response => {
        this.form = response.data;
        this.open = true;
        this.showIccid = false;
        this.title = '修改sim卡iccid号为"' + simIccid + '"的基本信息';
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.gmtCreate != null) {
            updateSim(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            getSim(this.form.simIccid).then(response => {
              if(response.data == null) {
                addSim(this.form).then(response => {
                  this.msgSuccess("正在添加，请稍等");
                  refreshSim(this.form.simIccid).then(response => {
                    this.msgSuccess("添加成功");
                    this.open = false;
                    this.getList();
                  }, response => {
                    this.msgError("sim卡iccid号无效");
                    delSim(this.form.simIccid);
                  });
                });
              } else if(response.data != null) {
                  this.msgError("该sim卡已存在");
              }
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const simIccids = row.simIccid || this.ids;
      this.$confirm('是否确认删除sim卡iccid号为"' + simIccids + '"的基本信息?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delSim(simIccids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
    },
    /** 更新按钮操作 */
    handleRefresh(row) {
      const simIccids = row.simIccid || this.ids;
      this.msgSuccess("正在更新，请稍后");
      refreshSim(simIccids).then(response => {
        this.getList();
        this.msgSuccess("更新成功");
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('terminal/sim/export', {
        ...this.queryParams
      }, `terminal_sim.xlsx`)
    }
  }
};
</script>
